package tan.dft.dangnhap;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import tan.dft.gps.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class MainActivityDangNhap extends Activity {

	private EditText uName;
	private EditText uPass;
	private Button loginBtn;
	private ArrayList<Dangnhap> array;
	private ViewFlipper flipper;
	public int idmay;
	public String myjsonstring, user, password, usern, passwordn;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.viewlipper);
		// buoc 2: khoi tao
		flipper = (ViewFlipper) findViewById(R.id.viewFlipper);
		array = new ArrayList<Dangnhap>();

		createArray();
		setUpViews();

	}

	@Override
	protected void onResume() {
		super.onResume();
		uName.setText("Enter User Name");
		uName.setTextColor(Color.parseColor("#888888"));
		uPass.setText("Enter Password");
		uPass.setInputType(InputType.TYPE_CLASS_TEXT);
		uPass.setTextColor(Color.parseColor("#888888"));
	}

	public final boolean isInternetOn() {

		ConnectivityManager connec = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		// ARE WE CONNECTED TO THE NET
		if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED
				|| connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING
				|| connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING
				|| connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) {
			// MESSAGE TO SCREEN FOR TESTING (IF REQ)
			// Toast.makeText(this, connectionType + ” connected”,
			// Toast.LENGTH_SHORT).show();
			return true;
		} else if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
				|| connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {

			return false;
		}
		return false;
	}

	private void setUpViews() {
		uName = (EditText) findViewById(R.id.usrNameTxt);
		uPass = (EditText) findViewById(R.id.usrPassTxt);

		uName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (uName.getText().toString()
						.equalsIgnoreCase("Enter User Name")) {
					if (hasFocus) {
						uName.setTextColor(Color.parseColor("#000000"));
						uName.setText("");
					}
				} else if (uName.getText().toString().equalsIgnoreCase("")) {
					uName.setTextColor(Color.parseColor("#888888"));
					uName.setText("Enter User Name");
				}
			}
		});

		uPass.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (uPass.getText().toString()
						.equalsIgnoreCase("Enter Password")) {
					if (hasFocus) {
						uPass.setInputType(InputType.TYPE_CLASS_TEXT
								| InputType.TYPE_TEXT_VARIATION_PASSWORD);
						uPass.setTextColor(Color.parseColor("#000000"));
						uPass.setText("");
					}
				}

				else if (uPass.getText().toString().equalsIgnoreCase("")) {
					uPass.setInputType(InputType.TYPE_CLASS_TEXT);
					uPass.setTextColor(Color.parseColor("#888888"));
					uPass.setText("Enter Password");
				}

			}
		});

		loginBtn = (Button) findViewById(R.id.loginBtn);

		loginBtn.setFocusableInTouchMode(false);
		loginBtn.requestFocus();

		loginBtn.setOnClickListener(new OnClickListener() {

			private String pass, name;

			@Override
			public void onClick(View v) {
				name = uName.getText().toString();
				pass = uPass.getText().toString();
				int n = array.size();

				for (int i = 0; i < n; i++) {

					int idmay = (Integer) array.get(i).getIdmay();
					String uString = array.get(i).getUser();
					String uString2 = array.get(i).getPassword();

					if (name.equalsIgnoreCase(uString)
							&& pass.equalsIgnoreCase(uString2)) {

						Intent intent = new Intent(MainActivityDangNhap.this,
								tan.dft.gps.MainActivity.class);
						Bundle bundle = new Bundle();
						bundle.putInt("idmay", idmay);
						intent.putExtras(bundle);
						startActivity(intent);
						
						flipper.setInAnimation(MainActivityDangNhap.this,
								R.anim.innext);
						flipper.setOutAnimation(MainActivityDangNhap.this,
								R.anim.outnext);
						flipper.showNext();
						break;
					} else if (i == n - 1) {
						Toast.makeText(getApplicationContext(),
								"User Or PassWord Error", Toast.LENGTH_LONG)
								.show();
					}
				}
			}
		});

	}

	private void createArray() {

		// Reading text file from assets folder
		StringBuffer sb = new StringBuffer();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(getAssets().open(
					"dangnhap.txt")));
			String temp;
			while ((temp = br.readLine()) != null)
				sb.append(temp);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close(); // stop reading
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		myjsonstring = sb.toString();
		try {
			JSONObject jsonObjMain = new JSONObject(myjsonstring);
			JSONArray jsonArray = jsonObjMain.getJSONArray("dangnhap");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObj = jsonArray.getJSONObject(i);
				idmay = jsonObj.getInt("idmay");
				user = jsonObj.getString("user");
				password = jsonObj.getString("password");

				Dangnhap dangnhap = new Dangnhap(idmay, user, password);
				array.add(dangnhap);

			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

}
